package net.kunyv.util;

import freemarker.template.*;
import net.kunyv.entity.DaoEntity;
import net.kunyv.entity.EntityColumn;

import java.io.*;
import java.util.*;

public class GenJava {
    public GenJava() {
    }

    public Configuration initConfiguration() throws IOException {
        String dir = System.getProperty("user.dir")+"/src/";

        Configuration cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File(dir));
        cfg.setObjectWrapper(new DefaultObjectWrapper());
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
        return cfg;
    }

    public Map<String, Object> initDateMap(List<EntityColumn> list, List<EntityColumn> keylist, DaoEntity de, String entityname) {
        Map<String, Object> data = new HashMap();
        List<EntityColumn> noFormHiddenlist = new ArrayList();
        Iterator var7 = list.iterator();

        while(var7.hasNext()) {
            EntityColumn ec = (EntityColumn)var7.next();
            if (!ec.getFormhidden().equals("true")) {
                noFormHiddenlist.add(ec);
            }
        }

        data.put("entityNam", entityname);
        data.put("columnEntitys", list);
        data.put("noFormHiddenlist", noFormHiddenlist);
        data.put("de", de);
        data.put("tablekeys", keylist);
        data.put("keySize", keylist.size());
        return data;
    }

    public void GenFileByFreemarker(String freemarkerfile, String entitydir, Configuration cfg, Map<String, Object> data, DaoEntity de, String filename) throws IOException, TemplateException {
        File f = new File(entitydir);
        if (!f.exists() || !f.isDirectory()) {
            f.mkdirs();
        }
        System.out.println("------------------------freemarkerfile:"+freemarkerfile);

        Template template = cfg.getTemplate(freemarkerfile, "UTF-8");
        Writer out = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8");
        template.process(data, out);
        out.flush();
        out.close();
    }

    public String GenJavaEntityByAno(List<EntityColumn> list, List<EntityColumn> keylist, DaoEntity de) throws IOException, TemplateException {
        String entityname = de.getEntityName();
        Configuration cfg = this.initConfiguration();


        Map<String, Object> data = this.initDateMap(list, keylist, de, entityname);
        String dir = de.getPackageName().replace(".", "/") + "/modules/" + de.getPackageNameSuffix().replace(".", "/");
        String entitydir = de.getDirectory() + "/" + entityname + "/java/" + dir + "/entity/";
        String controlldir = de.getDirectory() + "/" + entityname + "/java/" + dir + "/controller/";
        String javamapper = de.getDirectory() + "/" + entityname + "/java/" + dir + "/mapper/";
        String servicedir = de.getDirectory() + "/" + entityname + "/java/" + dir + "/service/";
        String mapperdir = de.getDirectory() + "/" + entityname + "/resources/mappings/" + de.getPackageNameSuffix().replace(".", "/") + "/";
        String filename = entitydir + "/" + entityname + ".java";



        this.GenFileByFreemarker("freemarkerTemplate/Entity.ftl", entitydir, cfg, data, de, filename);
        filename = mapperdir + "/" + entityname + "Mapper.xml";
        this.GenFileByFreemarker("freemarkerTemplate/Xml.ftl", mapperdir, cfg, data, de, filename);
        filename = javamapper + "/" + entityname + "Mapper.java";
        this.GenFileByFreemarker("freemarkerTemplate/Mapper.ftl", javamapper, cfg, data, de, filename);
        filename = servicedir + "/" + entityname + "Service.java";
        this.GenFileByFreemarker("freemarkerTemplate/Service.ftl", servicedir, cfg, data, de, filename);
        filename = controlldir + "/" + entityname + "Controller.java";
        this.GenFileByFreemarker("freemarkerTemplate/Controller.ftl", controlldir, cfg, data, de, filename);
        String sqldir = de.getDirectory() + "/" + entityname + "/";
        filename = sqldir + "/建表语句.sql";
        this.GenFileByFreemarker("freemarkerTemplate/Sql.ftl", sqldir, cfg, data, de, filename);
        if (keylist.size() > 1) {
        }

        return "成功生成java文件\r\n";
    }

    public String GenJSP(List<EntityColumn> list, List<EntityColumn> keylist, DaoEntity de) throws IOException, TemplateException {
        String entityname = de.getEntityName();
        Configuration cfg = this.initConfiguration();
        Map<String, Object> data = this.initDateMap(list, keylist, de, entityname);
        String jspdir = de.getDirectory() + "/" + entityname + "/webapp/WEB-INF/views/modules/" + de.getPackageNameSuffix().replace(".", "/") + "/";
        String filename = "";
        filename = jspdir + entityname + ".jsp";
        this.GenFileByFreemarker("freemarkerTemplate/EasyUIJsp.ftl", jspdir, cfg, data, de, filename);
        filename = jspdir + "/js/" + entityname + "Js.jsp";
        this.GenFileByFreemarker("freemarkerTemplate/EasyUIJs.ftl", jspdir + "/js/", cfg, data, de, filename);
        return "成功生成jsp和js文件\r\n";
    }

    public String GenJavaEntity(List<EntityColumn> list, List<EntityColumn> keylist, DaoEntity de) {
        try {
            return "成功生成hbm文件\r\n";
        } catch (Exception var5) {
            var5.printStackTrace();
            return PrintStackTrace.getStackTrace(var5);
        }
    }
}
